package cc.rengu.oltp.service.common.dao.impl;

import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.TxnNoticeInfoMapper;
import cc.rengu.oltp.service.common.entity.TxnNoticeInfo;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 业务报文信息表操作接口实现类
 * Created by 王成 on 2020/7/22
 */
public class TxnNoticeInfoMapperImpl implements TxnNoticeInfoMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    public TxnNoticeInfoMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        }
    }

    @Override
    public int insertTxnNoticeInfo(TxnNoticeInfo txnNoticeInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertTxnNoticeInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, txnNoticeInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记交易通知信息失败,instId:<{}>,sysSeqNum:<{}>,mchntNo:<{}>,noticeType:<{}>,noticeTarget:<{}>,iReturnCode:<{}>",
                    txnNoticeInfo.getInstId(), txnNoticeInfo.getSysSeqNum(), txnNoticeInfo.getMchntNo(), txnNoticeInfo.getNoticeType(), txnNoticeInfo.getNoticeTarget(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public TxnNoticeInfo selectTxnNoticeInfoByPrimaryKey(String instId, String sysSeqNum, String mchntNo, String noticeType, String noticeTarget) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTxnNoticeInfoByPrimaryKey");
        TxnNoticeInfo txnNoticeInfo = new TxnNoticeInfo();
        txnNoticeInfo.setInstId(instId);
        txnNoticeInfo.setSysSeqNum(sysSeqNum);
        txnNoticeInfo.setMchntNo(mchntNo);
        txnNoticeInfo.setNoticeType(noticeType);
        txnNoticeInfo.setNoticeTarget(noticeTarget);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, txnNoticeInfo, txnNoticeInfo);
        if (0 != iReturnCode) {
            rglog.warn("根据主键查询交易通知登记信息失败,,instId:<{}>,sysSeqNum:<{}>,mchntNo:<{}>,noticeType:<{}>,noticeTarget:<{}>,iReturnCode:<{}>",
                    txnNoticeInfo.getInstId(), txnNoticeInfo.getSysSeqNum(), txnNoticeInfo.getMchntNo(), txnNoticeInfo.getNoticeType(), txnNoticeInfo.getNoticeTarget(), iReturnCode);
            return null;
        }
        return txnNoticeInfo;
    }

    @Override
    public List<TxnNoticeInfo> selectTxnNoticeInfoBySysSeqNum(String instId, String sysSeqNum) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTxnNoticeInfoBySysSeqNum");
        TxnNoticeInfo txnNoticeInfo = new TxnNoticeInfo();
        txnNoticeInfo.setInstId(instId);
        txnNoticeInfo.setSysSeqNum(sysSeqNum);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, txnNoticeInfo, TxnNoticeInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.warn("根据系统流水号获取交易通知登记信息失败,instId:<{}>,sysSeqNum:<{}>", instId, sysSeqNum);
            return null;
        }
        return objectList.stream().map(item -> (TxnNoticeInfo) item).collect(Collectors.toList());
    }

    @Override
    public int updateTxnNoticeInfoByPrimaryKey(TxnNoticeInfo txnNoticeInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateTxnNoticeInfoByPrimaryKey");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, txnNoticeInfo, null);
        if (0 != iReturnCode) {
            rglog.error("更新交易通知信息失败,instId:<{}>,sysSeqNum:<{}>,mchntNo:<{}>,noticeType:<{}>,noticeTarget:<{}>,iReturnCode:<{}>",
                    txnNoticeInfo.getInstId(), txnNoticeInfo.getSysSeqNum(), txnNoticeInfo.getMchntNo(), txnNoticeInfo.getNoticeType(), txnNoticeInfo.getNoticeTarget(), iReturnCode);
        }
        return iReturnCode;
    }
}
